package com.expriment.aes;

import java.security.Key;
import com.algorithm.core.AESAlgorithm;
import com.algorithm.core.FHOPEAlgorithm;
import com.algorithm.core.KeyManager;


public class ExperimentWithAES {

    public void experimentWithAES() {
        try {
            long lstart = 0;
            long lendTime = 0;
            // 1.----------首先测试FHOPE算法的加解密时间------------
            long encTimeOfFHOPE = 0;
            long decTimeOfFHOPE = 0;
            FHOPEAlgorithm fhopeAlg = new FHOPEAlgorithm();
            // FHOPE加密时间
            lstart = System.nanoTime();
            int encNum = 5;
            double sens = 0.1;
            WRFile wr = new WRFile();
            double[][] keys = fhopeAlg.keyGenerate(encNum);
            wr.writeKeysIntoFile(keys);
            double[][] noises = fhopeAlg.noiseGenerate(encNum, sens, keys);
            double newExtraNoise = fhopeAlg.extraNoiseGenerate();
            wr.writeENIntoFile(newExtraNoise);
            double value = 23.8;
            double[] encValue = fhopeAlg.encryptFunc(value, keys, noises, encNum);
            lendTime = System.nanoTime();
            encTimeOfFHOPE = lendTime - lstart;

            // FHOPE解密时间
            lstart = System.nanoTime();
            fhopeAlg.decryptFunc(encValue, keys, encNum);
            lendTime = System.nanoTime();
            decTimeOfFHOPE = lendTime - lstart;

            // 2.----------其次测试AES算法的加密时间------------------
            lstart = System.nanoTime();
            long encTimeOfAES = 0;
            long decTimeOfAES = 0;
            String content = "12345678";
            String password = "1234567812345678";
            // AES加密时间
            Key detKey = KeyManager.generateDETKey(password, "id", "det");
            String encryptResult = AESAlgorithm.encrypt(content, detKey);
            lendTime = System.nanoTime();
            encTimeOfAES = lendTime - lstart;
            // AES解密时间
            lstart = System.nanoTime();
            AESAlgorithm.decrypt(encryptResult, detKey);
            lendTime = System.nanoTime();
            decTimeOfAES = lendTime - lstart;

            // 3.将结果输出到Excel表格中
            ResultOfAES expOut = new ResultOfAES();
            expOut.experimentWithAESOutput(encTimeOfFHOPE, encTimeOfAES,decTimeOfFHOPE, decTimeOfAES);
            System.out.println("实验一:获取FHOPE和AES加解密时间实验完成!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
